/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.slogger.appender;

import com.hyts.slogger.base.SAppender;
import com.hyts.slogger.printer.base.LogPrinter;

import java.io.IOException;
import java.io.StringWriter;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.slogger.appender
 * @author:LiBo/Alex
 * @create-date:2019-08-07 10:30
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: 文本日志填充器操作服务组件
 */
public class TextLoggerAppender extends SAppender{


    StringWriter stringWriter = new StringWriter();


    public TextLoggerAppender(LogPrinter logPrinter) {
        super(logPrinter);
    }


    @Override
    public Appendable append(CharSequence csq) throws IOException {
        stringWriter.append(csq);
        return this;
    }

    @Override
    public Appendable append(CharSequence csq, int start, int end) throws IOException {
        stringWriter.append(csq,start,end);
        return this;
    }

    @Override
    public Appendable append(char c) throws IOException {
        stringWriter.append(c);
        return this;
    }

    @Override
    public void flush(boolean isError) {
        if(isError) {
            logPrinter.err(stringWriter.toString());
        }else {
            logPrinter.out(stringWriter.toString());
        }
        stringWriter = new StringWriter();
    }

}
